package com.example.demo.controller;

import com.example.demo.dto.monDTO.DBObjectList;
import com.example.demo.dto.monDTO.PersonResultMongo;
import com.example.demo.entity.UserInfo;
import com.example.demo.enums.UserEnum;
import com.example.demo.service.PersonResultService;
import com.example.demo.service.UserInfoService;
import com.example.demo.vo.RecordVO;
import com.example.demo.vo.UserResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
@Api(tags = "获取基本信息(用户信息,识别记录)")
public class RecordConroller {
    @Autowired
    private PersonResultService personResultService;
    @Autowired
    private UserInfoService userInfoService;
    @PostMapping("/getUserInfo")
    @ApiOperation("得到用户信息")
    public UserResultVO getUserInfo(@RequestParam("openid") String openid){
        UserInfo userInfo = userInfoService.findByOpenid(openid);
        UserResultVO userResultVO = new UserResultVO();
        if(userInfo==null){
            userResultVO.setCode(UserEnum.getUserError.getCode());
            userResultVO.setMsg(UserEnum.getUserError.getMessage());
            userResultVO.setUserInfo(null);
            return  userResultVO;
        }else{
            userResultVO.setCode(UserEnum.getUserRight.getCode());
            userResultVO.setMsg(UserEnum.getUserRight.getMessage());
            userResultVO.setUserInfo(userInfo);
            return  userResultVO;
        }
    }
    @PostMapping("/getRecord")
    @ApiOperation("得到用户查询记录")
    public DBObjectList getRecord(@ApiParam("用户标识openid") @RequestParam("openid") String openid,
                                    @ApiParam("限制条数") @RequestParam(value = "limit") Integer limit,
                                    @ApiParam("起始点,也就是从第几条开始查") @RequestParam(value = "skip") Integer skip){
        DBObjectList dbObjectList  = personResultService.getRecordList(openid,limit,skip);
        return dbObjectList;
    }
    @PostMapping("/getRecord2")
    @ApiOperation("得到用户查询记录")
    public List<PersonResultMongo> getRecord2(@ApiParam("用户标识openid") @RequestParam("openid") String openid,
                                              @ApiParam("一页要查询多少条") @RequestParam(value = "size") Integer size,
                                              @ApiParam("第几页") @RequestParam(value = "page") Integer page){
        PageRequest request = new PageRequest(page-1,size);
        List<PersonResultMongo> personResultMongos  = personResultService.getResultMongo(openid,request);
        return personResultMongos;
    }
}
